const createStore = (reducer, enhancer) => {
    if(enhancer){
       return  enhancer(createStore)(reducer)
    }
    let state;
    let listener = []

    const getState = () => {
        return state
    }
    const subscribe = (callback) => {
        listener.push(callback)

        return ()=>{
            const index = listener.indexOf(callback)
            listener.splice(index,1)
        }
    }

    const dispatch = (action) => {
        state = reducer(state, action)
        listener.forEach(callback=>callback())
        return action
    }
    dispatch({})
    return {
        getState,
        dispatch,
        subscribe
    }
}

export default createStore